/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bsc.war.form;


import bsc.ejb.entity.Attachment;
import bsc.ejb.entity.Note;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.ScoreCard;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.FileUtils;
import bsc.war.service.ScoreCardUtils;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

//org.apache.commons.validator.GenericValidator

/**
 *
 * @author luisZavaleta
 */
public class NewNote extends AbstractController  {

     @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
         HttpServletResponse response) throws Exception {

         ModelAndView mav = new ModelAndView("renderXml");
         try {

             //scorecardId="+scorecardId+"&title="+title+"&note="+note;
             Map<String, Object> parameterValues = FileUtils.getMultipartParameters(request);
             Object file = parameterValues.get("file");
             byte[] attachmentFile = file == null || !(file instanceof byte[]) ? null : (byte[])file;
             String attachmentFileName = (String) parameterValues.get("file_filename");
             
             String idScoreCard = (String)parameterValues.get("scorecardId");
             // System.out.println("Attach es: " + attachmentFileName);
             // System.out.println("El id del ScoreCard es: " + idScoreCard);

             HttpSession session = request.getSession();
             Person person = (Person)session.getAttribute("person");

             DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
             ScoreCard scorecard= delegate.findScoreCard(Long.parseLong(idScoreCard));

             Note note = (Note)Factory.getBean("Note");
             note.setTitle((String)parameterValues.get("title"));
             note.setNote((String)parameterValues.get("note"));
             note.setScoreCard(scorecard);
             note.setPerson(person);
             note.setNoteDate(ScoreCardUtils.generateActualDate());
             
             delegate.createNote(note);

             if(attachmentFile != null && attachmentFile.length > 0){
                 
                 Attachment attachment = (Attachment)Factory.getBean("Attachment");
                 attachment.setNote(note);
                 delegate.createAttachment(attachment);

                 String fileName = FileUtils.generateFilename(attachment, FileUtils.FileType.ATTACHMENT) + "_" + attachmentFileName;
                 boolean fileSaved = FileUtils.saveFile(attachmentFile, fileName, FileUtils.FileType.ATTACHMENT);
                 if(!fileSaved)delegate.deleteAttachment(attachment);
                 else {
                     attachment.setFilename(fileName);
                     attachment.setAttachment(FileUtils.generateFileURL(fileName, FileUtils.FileType.ATTACHMENT));
                     delegate.updateAttachment(attachment);
                 }
             }

             mav.addObject("closeWindow",true);
             
         } catch (Exception e) {
            e.printStackTrace();
         }
         
         return mav;
    }

}
